package com.blogsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blogsystem.entity.Article;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ArticleMapper extends BaseMapper<Article> {

    @Select({
            "<script>",
            "SELECT",
            "a.article_id, a.title, a.content, a.category_id, a.user_id, a.create_time,",
            "u.username, c.name AS categoryName, cc.total_clicks AS clickCount",
            "FROM article a",
            "LEFT JOIN user u ON a.user_id = u.user_id",
            "LEFT JOIN category c ON a.category_id = c.category_id",
            "LEFT JOIN click_count cc ON a.article_id = cc.article_id",
            "WHERE a.status = 1",
            "<if test='categoryId != null'>AND a.category_id = #{categoryId}</if>",
            "ORDER BY a.create_time DESC",
            "LIMIT #{offset}, #{size}",
            "</script>"
    })
    @Results({
            @Result(column = "username", property = "authorName"), // 将username映射到articleName
            @Result(column = "categoryName", property = "categoryName"),
            @Result(column = "clickCount", property = "clickCount")
    })
    List<Article> selectArticlesByPage(
            @Param("categoryId") Integer categoryId,
            @Param("offset") int offset,
            @Param("size") int size);
}
